from rest_framework.views import APIView
from rest_framework.response import Response

from apps.index.models import Workslb, IndexClassify
from apps.index.serializers import WorkslbSerializer, IndexClassifySerializer
from apps.works.models import WorksList
from apps.works.serializers import WorksListSerializer


class IndexView(APIView):
    def get(self, request, *args, **kwargs):
        works = Workslb.objects.filter(status=True)  # 一级分类
        serializer = WorkslbSerializer(works, many=True)
        imgList = serializer.data
        category_index = IndexClassify.objects.filter(status=True)
        ic_serializer = IndexClassifySerializer(category_index, many=True)
        navList = ic_serializer.data
        works_list = WorksList.objects.filter(status=True).order_by("-tracks")
        cnx_serializer = WorksListSerializer(works_list, many=True)
        cnxList = cnx_serializer.data
        ys_list = WorksList.objects.filter(category_v2=1, status=True).order_by("-tracks")
        ysList = WorksListSerializer(ys_list, many=True).data
        xs_list = WorksList.objects.filter(category_v2=4, status=True).order_by("-tracks")
        xsList = WorksListSerializer(xs_list, many=True).data
        yy_list = WorksList.objects.filter(category_v2=9, status=True).order_by("-tracks")
        yyList = WorksListSerializer(yy_list, many=True).data
        if len(cnxList) > 3:
            cnxList = cnxList[0:3]
        if len(ysList) > 3:
            ysList = ysList[0:3]
        if len(xsList) > 3:
            xsList = xsList[0:3]
        if len(yyList) > 3:
            yyList = yyList[0:3]
        # 2. 返回应答
        response_data = {
            'imgList': imgList,  # 轮播图
            'navList': navList,  # 宫格导航
            'cnxList': cnxList,  # 猜你喜欢
            'ysList': ysList,  # 有声小说
            'xsList': xsList,  # 相声评书
            'yyList': yyList,  # 音乐
        }

        return Response(response_data)


class CatAllView(APIView):
    """查看全部"""

    def get(self, request, *args, **kwargs):
        id = self.kwargs['pk']
        print(type(id))
        try:
            if id == "1":
                works_list = WorksList.objects.filter(status=True).order_by("-tracks")
            elif id == "2":
                works_list = WorksList.objects.filter(category_v2=1, status=True).order_by("-tracks")
            elif id == "3":
                works_list = WorksList.objects.filter(category_v2=4, status=True).order_by("-tracks")
            else:
                works_list = WorksList.objects.filter(category_v2=9, status=True).order_by("-tracks")
            serializer = WorksListSerializer(works_list, many=True)
            works = serializer.data
        except:
            works = ''
        # print(title)
        # print(navTitle)
        # 2. 返回应答
        response_data = {
            'works': works,
        }

        return Response(response_data)
